// pages/SCM/store/goodsAdd/goodsAdd.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  data: {
    hideFlag: true,
    address: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.data.id = parseInt(options.id);
      this.receiveAddressItem();
    }
    this.data.applyUid = options.applyUid?options.applyUid:'';
    if (options.type) {
      this.setData({
        type: options.type
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    that.personalModal = that.selectComponent("#personalModal");
    that.toast = that.selectComponent("#toast");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    //选择医院完后，获取医院下人员。在这里处理。

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 添加常用联系人
   */
  newRecentUser: function (e) {
    let formData = e.detail.value;
    if (!formData.userName) {
      that.toast.showToast('填写姓名');
      return
    }
    if (!formData.userPhone) {
      that.toast.showToast('填写手机号码');
      return
    }
    if (!app.globalData.phoneReg.test(formData.userPhone)) {
      that.toast.showToast('填写11位手机号码');
      return
    }
    lbbService.addStaff(formData, function (res) {//todo
      wx.showToast({
        title: '提交成功',
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 1500)
    })
  },

  receiveAddressItem: function () {
    let that = this;
    lbbService.receiveAddressItem({
      id: that.data.id
    }, function (res) {
      that.setData({
        address: res.data
      });
    });
  },

  formSubmit: function (e) {
    let that = this
    console.log(e)
    let formData = e.detail.value
    that.setData({
      formData: formData
    })
    if (!formData.receivePerson) {
      that.toast.showToast('填写收货人');
      return
    }
    if (!formData.receivePhone) {
      that.toast.showToast('填写联系电话');
      return
    }
    if (!app.globalData.phoneReg.test(formData.receivePhone)) {
      that.toast.showToast('填写11位联系电话');
      return
    }
    if (!formData.receiveAddress) {
      that.toast.showToast('填写联系地址');
      return
    }

    lbbService.saveReceiveAddress(formData, function (res) {
      wx.showToast({
        title: '提交成功',
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        })
      }, 1500)
    })
  },





  chooseCategory: function (e) {
    console.log(e)
    let that = this
    let category = e.currentTarget.dataset.category;
    that.setData({
      categoryId: category.id,
      categoryName: category.name
    });
  },

  showModel: function () {
    this.setData({
      hideFlag: false
    })
  },

  hideModel: function () {
    this.setData({
      hideFlag: true
    })
  },



})
